#!/bin/bash

# test_trim_specs_display.sh - 测试Trim规格数据显示修复
# 修复前端显示[object Object]问题

echo "🔥 Trim规格数据显示修复验证脚本"
echo "================================================"

# 服务器信息
SERVER_URL="http://localhost:5700"
API_BASE="${SERVER_URL}/api"

# 测试车型
TEST_MODELS=("B-MAX" "Focus" "Puma")

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 检查服务器状态
check_server() {
    echo -n "检查服务器状态... "
    if curl -s "$SERVER_URL/health" > /dev/null 2>&1; then
        echo -e "${GREEN}✅ 服务器运行正常${NC}"
        return 0
    else
        echo -e "${RED}❌ 服务器未运行${NC}"
        exit 1
    fi
}

# 测试单个车型的规格数据
test_trim_specs_data() {
    local model="$1"
    
    echo ""
    echo -e "${BLUE}📋 测试车型: $model${NC}"
    echo "----------------------------------------"
    
    # 获取trim数据
    echo -n "  获取trim数据... "
    trims_response=$(curl -s "$API_BASE/get_trims?model=$model")
    
    if echo "$trims_response" | grep -q "error"; then
        echo -e "${RED}❌ 获取失败${NC}"
        echo "    错误信息: $trims_response"
        return 1
    fi
    
    # 检查响应格式
    trim_count=$(echo "$trims_response" | jq -r '.total_trims' 2>/dev/null || echo "0")
    echo -e "${GREEN}✅ 找到 $trim_count 个配置${NC}"
    
    if [ "$trim_count" -gt 0 ]; then
        # 分析第一个trim的规格数据
        echo "  分析规格数据结构..."
        
        # 获取第一个trim的规格数据
        first_trim_specs=$(echo "$trims_response" | jq -r '.trims[0].specifications' 2>/dev/null)
        
        if [ "$first_trim_specs" != "null" ] && [ "$first_trim_specs" != "" ]; then
            echo "    规格数据类型: $(echo "$first_trim_specs" | jq -r 'type' 2>/dev/null)"
            
            # 检查是否包含分类
            categories=$(echo "$first_trim_specs" | jq -r 'keys[]' 2>/dev/null | head -5)
            echo "    规格分类: $categories"
            
            # 检查第一个分类的结构
            first_category=$(echo "$first_trim_specs" | jq -r 'keys[0]' 2>/dev/null)
            if [ "$first_category" != "null" ] && [ "$first_category" != "" ]; then
                echo "    第一个分类 ($first_category):"
                first_category_data=$(echo "$first_trim_specs" | jq -r ".\"$first_category\"" 2>/dev/null)
                echo "      数据类型: $(echo "$first_category_data" | jq -r 'type' 2>/dev/null)"
                
                if echo "$first_category_data" | jq -e 'type == "array"' > /dev/null 2>&1; then
                    item_count=$(echo "$first_category_data" | jq '. | length' 2>/dev/null)
                    echo "      项目数量: $item_count"
                    
                    if [ "$item_count" -gt 0 ]; then
                        echo "      第一项结构:"
                        echo "$first_category_data" | jq '.[0] | {spec_name, spec_value, spec_unit}' 2>/dev/null | sed 's/^/        /'
                    fi
                fi
            fi
            
            echo -e "    ${GREEN}✅ 规格数据结构正常${NC}"
        else
            echo -e "    ${RED}❌ 规格数据为空${NC}"
        fi
        
        # 检查specs_count
        specs_count=$(echo "$trims_response" | jq -r '.trims[0].specs_count' 2>/dev/null)
        echo "    规格项目数量: $specs_count"
        
    else
        echo -e "  ${YELLOW}⚠️ 该车型无配置数据${NC}"
        return 1
    fi
    
    return 0
}

# 测试前端JavaScript修复
test_javascript_fix() {
    echo ""
    echo -e "${BLUE}🔧 检查JavaScript修复${NC}"
    echo "----------------------------------------"
    
    # 检查新增的函数
    echo -n "  检查renderCategoryArraySpecs函数... "
    if grep -q "renderCategoryArraySpecs" "web/assets/js/trim.js"; then
        echo -e "${GREEN}✅ 已添加${NC}"
    else
        echo -e "${RED}❌ 缺失${NC}"
        return 1
    fi
    
    echo -n "  检查JSON解析处理... "
    if grep -q "JSON.parse(specsData)" "web/assets/js/trim.js"; then
        echo -e "${GREEN}✅ 已添加${NC}"
    else
        echo -e "${RED}❌ 缺失${NC}"
        return 1
    fi
    
    echo -n "  检查调试日志... "
    if grep -q "console.log.*渲染规格数据" "web/assets/js/trim.js"; then
        echo -e "${GREEN}✅ 已添加${NC}"
    else
        echo -e "${RED}❌ 缺失${NC}"
        return 1
    fi
    
    echo -n "  检查分类数组处理... "
    if grep -q "spec_name" "web/assets/js/trim.js" && grep -q "spec_value" "web/assets/js/trim.js"; then
        echo -e "${GREEN}✅ 已添加${NC}"
    else
        echo -e "${RED}❌ 缺失${NC}"
        return 1
    fi
    
    return 0
}

# 生成修复建议
generate_fix_suggestions() {
    echo ""
    echo -e "${BLUE}💡 修复建议和验证步骤${NC}"
    echo "----------------------------------------"
    echo "1. 在浏览器中访问: $SERVER_URL/web/"
    echo "2. 点击任意车型的'查看配置'按钮"
    echo "3. 在模态框中检查规格数据是否正常显示"
    echo "4. 按F12打开开发者工具，查看Console日志"
    echo "5. 验证规格数据不再显示为[object Object]"
    echo ""
    echo "预期结果:"
    echo "  ✅ 规格数据按分类正确显示"
    echo "  ✅ 每个规格项显示名称和值"
    echo "  ✅ Console有调试日志输出"
    echo "  ✅ 无JavaScript错误"
}

# 生成测试报告
generate_test_report() {
    local total_tests="$1"
    local passed_tests="$2"
    local failed_tests="$3"
    
    echo ""
    echo "================================================"
    echo -e "${BLUE}📊 Trim规格显示修复测试报告${NC}"
    echo "================================================"
    echo "测试项目: $total_tests"
    echo -e "通过项目: ${GREEN}$passed_tests${NC}"
    echo -e "失败项目: ${RED}$failed_tests${NC}"
    
    if [ "$failed_tests" -eq 0 ]; then
        echo -e "${GREEN}🎉 Trim规格数据显示问题已修复！${NC}"
        echo ""
        echo "✅ API数据结构分析完成"
        echo "✅ JavaScript渲染函数已修复"
        echo "✅ 分类数组格式支持已添加"
        echo "✅ JSON解析错误处理完善"
        echo "✅ 调试日志已添加"
    else
        echo -e "${RED}❌ 存在问题需要进一步修复${NC}"
    fi
    
    generate_fix_suggestions
}

# 主测试流程
main() {
    echo "开始测试时间: $(date)"
    echo ""
    
    # 检查服务器
    check_server
    
    # 初始化计数器
    total_tests=0
    passed_tests=0
    failed_tests=0
    
    # 测试JavaScript修复
    ((total_tests++))
    if test_javascript_fix; then
        ((passed_tests++))
    else
        ((failed_tests++))
    fi
    
    # 测试每个车型的规格数据
    for model in "${TEST_MODELS[@]}"; do
        ((total_tests++))
        
        if test_trim_specs_data "$model"; then
            ((passed_tests++))
        else
            ((failed_tests++))
        fi
        
        sleep 0.5
    done
    
    # 生成报告
    generate_test_report "$total_tests" "$passed_tests" "$failed_tests"
    
    # 返回适当的退出码
    if [ "$failed_tests" -eq 0 ]; then
        exit 0
    else
        exit 1
    fi
}

# 运行主函数
main "$@"
